Legacy database

Submitted 3 years, 9 months ago
Ticket #35
Views 280
Language/Framework Python
Priority Low
Status Closed

I have 3 separate django projects and they share the same database.

1. I create a abstractuser model and everytime I login or register a user, it will generate a token using django-rest-knox.

2. I am trying to access the table of user(abstractuser) and knox using inspectdb from project1 to project2.

3. I successfully access and connect user from project1 to project2 but the knox_token is still blank

Do you have any idea how to implement this?

Submitted on Jul 08, 20
add a comment

2 Answers

Can you share your model and let me know what was the error you are getting exactly?

Submitted 3 years, 9 months ago

Is this still open issue?

Submitted 3 years, 9 months ago

yes. i am still having an issue using shared db across services

- RadySonabu 3 years, 9 months ago

can you share your model information. it would be great if you provide more details with the current code, right now its very hard to figure out whats going on your project. If required we can connect via zoom. but before that i need to take a look on your design and structure.

- Vengat 3 years, 9 months ago

Can you update us at the earliest?. So that we can help you.

- Vengat 3 years, 9 months ago

```

class Contact_A00(AbstractUser): contact_a00_rec = models.AutoField(primary_key=True)

contact_id = models.CharField(max_length=50, default='CON0000000000')
first_name = models.CharField(max_length=50, null=True)
middle_initial = models.CharField(max_length=50, null=True)
last_name = models.CharField(max_length=50, null=True)
address_1 = models.CharField(max_length=50, null=True)
barangay_district = models.CharField(max_length=50, null=True)
city_municipality = models.CharField(max_length=50, null=True)
postal_code = models.IntegerField( null=True)
province = models.CharField(max_length=50)
phone_1 = PhoneField(blank=True, null=True, help_text='Contact phone number')
phone_2 = PhoneField(null=True,blank=True, help_text='Alternate phone number 2')
email = models.EmailField(max_length=254, unique=True)
active_status = models.BooleanField(default=True,  null=True)
date_created = models.DateTimeField(auto_now_add=True, null=True)


headline = models.TextField(blank=True, null=True)
bio = models.TextField(blank=True, null=True)

REQUIRED_FIELDS = ['username']
USERNAME_FIELD = 'email'
def __str__(self):
    return f'{self.contact_a00_rec} {self.contact_id} {self.first_name} {self.last_name} | {self.phone_1}'

```

- RadySonabu 3 years, 9 months ago

As we didnt get any update on this. We have changed the ticket priority to LOW

- Vengat 3 years, 9 months ago

` class RegisterAPI(generics.GenericAPIView): # queryset = Contact_A00.objects.all() serializer_class = RegisterSerializer

def post(self, request, *args, **kwargs):

    serializer = self.get_serializer(data=request.data)
    serializer.is_valid(raise_exception=True)
    user = serializer.save()
    _, token = AuthToken.objects.create(user,)
    return Response({
        "token": token,
        "user": UserSerializer(user, context=self.get_serializer_context()).data,

    })`
- RadySonabu 3 years, 9 months ago

and I am calling that model to another service from a separate programmer. we share same database but different schema.

```

class AccountsContactA00(AbstractUser): password = models.CharField(max_length=128) last_login = models.DateTimeField(blank=True, null=True) is_superuser = models.BooleanField() username = models.CharField(unique=True, max_length=150) is_staff = models.BooleanField() is_active = models.BooleanField() date_joined = models.DateTimeField() contact_a00_rec = models.AutoField(primary_key=True) contact_id = models.CharField(max_length=50) first_name = models.CharField(max_length=50, blank=True, null=True) middle_initial = models.CharField(max_length=50, blank=True, null=True) last_name = models.CharField(max_length=50, blank=True, null=True) address_1 = models.CharField(max_length=50, blank=True, null=True) barangay_district = models.CharField(max_length=50, blank=True, null=True) city_municipality = models.CharField(max_length=50, blank=True, null=True) postal_code = models.IntegerField(blank=True, null=True) province = models.CharField(max_length=50) phone_1 = models.CharField(max_length=31, blank=True, null=True) phone_2 = models.CharField(max_length=31, blank=True, null=True) email = models.CharField(unique=True, max_length=254) active_status = models.BooleanField(blank=True, null=True) date_created = models.DateTimeField(blank=True, null=True)

REQUIRED_FIELDS = ['username']
USERNAME_FIELD = 'email'
class Meta:
    managed = False
    db_table = 'accounts_contact_a00'

class AccountsContactA00Groups(models.Model): contact_a00 = models.ForeignKey(AccountsContactA00, models.DO_NOTHING) group = models.ForeignKey('AuthGroup', models.DO_NOTHING)

class Meta:
    managed = False
    db_table = 'accounts_contact_a00_groups'
    unique_together = (('contact_a00', 'group'),)

class AccountsContactA00UserPermissions(models.Model): contact_a00 = models.ForeignKey(AccountsContactA00, models.DO_NOTHING) permission = models.ForeignKey('AuthPermission', models.DO_NOTHING)

class Meta:
    managed = False
    db_table = 'accounts_contact_a00_user_permissions'
    unique_together = (('contact_a00', 'permission'),)

class AuthGroup(models.Model): name = models.CharField(unique=True, max_length=150)

class Meta:
    managed = False
    db_table = 'auth_group'

class AuthGroupPermissions(models.Model): group = models.ForeignKey(AuthGroup, models.DO_NOTHING) permission = models.ForeignKey('AuthPermission', models.DO_NOTHING)

class Meta:
    managed = False
    db_table = 'auth_group_permissions'
    unique_together = (('group', 'permission'),)

class AuthPermission(models.Model): name = models.CharField(max_length=255) content_type = models.ForeignKey('DjangoContentType', models.DO_NOTHING) codename = models.CharField(max_length=100)

class Meta:
    managed = False
    db_table = 'auth_permission'
    unique_together = (('content_type', 'codename'),)

class DjangoAdminLog(models.Model): action_time = models.DateTimeField() object_id = models.TextField(blank=True, null=True) object_repr = models.CharField(max_length=200) action_flag = models.SmallIntegerField() change_message = models.TextField() content_type = models.ForeignKey('DjangoContentType', models.DO_NOTHING, blank=True, null=True) user = models.ForeignKey(AccountsContactA00, models.DO_NOTHING)

class Meta:
    managed = False
    db_table = 'django_admin_log'

class DjangoContentType(models.Model): app_label = models.CharField(max_length=100) model = models.CharField(max_length=100)

class Meta:
    managed = False
    db_table = 'django_content_type'
    unique_together = (('app_label', 'model'),)

class DjangoMigrations(models.Model): app = models.CharField(max_length=255) name = models.CharField(max_length=255) applied = models.DateTimeField()

class Meta:
    managed = False
    db_table = 'django_migrations'

class DjangoSession(models.Model): session_key = models.CharField(primary_key=True, max_length=40) session_data = models.TextField() expire_date = models.DateTimeField()

class Meta:
    managed = False
    db_table = 'django_session'

class KnoxAuthtoken(models.Model): digest = models.CharField(primary_key=True, max_length=128) salt = models.CharField(unique=True, max_length=16) created = models.DateTimeField() user = models.ForeignKey(AccountsContactA00, models.DO_NOTHING, db_column='user') expiry = models.DateTimeField(blank=True, null=True) token_key = models.CharField(max_length=8)

class Meta:
    managed = False
    db_table = 'knox_authtoken'

```

- RadySonabu 3 years, 9 months ago

the error I am getting is when I login to the admin site. I get some tables does not exists.

- RadySonabu 3 years, 9 months ago

Did you run the migrations on the database which you are tried to login . Please check this Django docs for more info Multiple databases

- Vengat 3 years, 9 months ago

yes.

- RadySonabu 3 years, 9 months ago

I run the migrations but I think there are problems from inheriting the custom user model table

- RadySonabu 3 years, 9 months ago

can we have a zoom meeting to discuss over this?.. with the details its very hard to figure out the issue. Or share the github link.

- Vengat 3 years, 9 months ago


Latest Blogs