I have an access database with two tables, members and residential areas, I'm using a tool to migrate the tables from access to postgres on railway.app. The problem is, when i run migrations from django, i get a relationship doesn't exist error. (I have a foreign key attribute in members referencing residential areas because each member is linked to a residential area.
I understand this is happening because when the data gets to postgres, it doesn't have a unique ID fields(the auto-generated ID from django), I've tried using custom fields as primary keys but nothing seems to work. Anyone with an idea how I can solve this issue?
Related
I am using a MySQL db in my flask app. I am using SQLAlchemy with pymysql too. The app is deployed on a web server(linux based - specifically Ubuntu). I am trying to apply migrations using flask-migrate but I keep on getting the error
(1170, u"BLOB/TEXT column 'user_id' used in key specification without a key length")
My primary keys look like this:
id = db.Column(db.String(32),default=lambda: str(uuid4().hex), primary_key=True)
The specific table where this error arises is an association table: The code is
user_association_table = db.Table('association', db.Model.metadata,
db.Column('user_id', db.String(32), db.ForeignKey('user.id')),
db.Column('article_id', db.String(32), db.ForeignKey('article.id'))
)
I have seen similar questions but they address how to fix it using MySQL directly. Since I have minimal control of how mysql is generated, how can I bypass this error?
Turns out the original MySQL db that I was trying to apply migrations to was not fully up to date with the size of the primary key --> db.String(32) Deleting and recreating the database made migrations work just fine.
If the database has data then it would be more painful to recreate the database without losing data.
I'm trying to setup a new server with foreign tables (using postgres_fdw) that weren't foreign tables previously, and I have some OneToOneFields pointing to these tables. This doesn't work out of the box - OneToOneFields use foreign keys, and postgres_fdw does not support foreign keys for foreign tables.
The foreign tables are in a read-only database on the same server.
Is there an easy way to get this working?
After a little more research, I think the answer is 'don't do that'. Handling foreign keys for foreign tables isn't implemented, and any solution that tried to bridge that gap would have serious performance and/or consistency issues.
pglogical looks like a better solution - instead of pulling the data in through postgres_fdw, replicate the tables into the new database so they're local. There are other projects for replicating just specific tables, but pglogical is included with PostgreSQL 10.
I am doing a poc in Django and i was trying to create the admin console module for inserting,updating and deleting records through django admin console through models and it was doing fine
I have 2 questions.
1.I need to have model objects for existing tables which needs to be present in a particular schema.say schema1.table1
Here as of now i was doing poc for public schema.
So can it be done in a fixed defined schema and if yes how.Any reference would be very helpful
2.Also i wanted to update few columns in the table through console and the rest of the columns will be done automatically like currentimestamp and created date etc.Is it possible through default django console and if yes kindly share any reference
Steps for 1
What i have done as of now is created a class in model.py with attributes as author,title,body,timeofpost
Then i used sqlmigrate after makemigrations app to create the table and after migrating have been using the admin console for django to insert and update the records for the table created.But this is for POC only.
Now i need to do the same but for existing tables with whom i can interact and insert or update record for those existing tables through admin console.
Also the tables are getting created in public schema by default.But i am using postgres and the existing tables are present in different schemas and i wanted to insert,update and delete for this existing tables.
I am stuck up here as i dont know how to configure model with existing database schema tables through which we can interact through django console and also for different schemas and not in public schema
Steps for 2:
Also i wanted the user to give input for few columns like suppose in this case time of creation is not required to be given as input by user .Rather it should be taken care when the database is updating or creating
Thanks
In order for Django to "interact" with an existing database you need to create a model for it which can be done automatically as shown here. This assumes that your "external" database isn't going to be changed often because you'll have to keep your models in sync which is tricky - there are other approaches if you need that.
As for working with multiple database schemas - is there a reason you can't put your POC table in the same database as the others? Django supports multiple databases, but it will be harder to setup. See here.
Finally, it sounds like you are interested in setting the Django default field attribute. For an example of current time see here.
Here's what I want to do: I need to migrate some models to another database modifying some of its attributes. The approach I came with was set another database setting in my DATABASES settings named 'production'.
Then I do this:
for cat_to, cat_from in category_names.items():
c_f = InRadarCategory.objects.get(name=cat_from)
c_t = InRadarCategory.objects.get(name=cat_to)
for ad in InRadarAd.objects.filter(category=c_f):
ad.pk = None
ad.category = InRadarCategory.objects.using('production').get(name=c_t.name)
ad.save(using='production')
Note that, before saving it to the other database, I modify it's category attribute with an object that's in the other database. After that, I try to push it in by calling save on ad instance, but using the production database.
That gives me the error:
instance is on database "default", value is on database "production"
Django docs states here that:
Django doesn’t currently provide any support for foreign key or many-to-many relationships spanning multiple databases. If you have used a router to partition models to different databases, any foreign key and many-to-many relationships defined by those models must be internal to a single database.
Question is: Am I at the right path but missing something silly? Is there a alternative approach to achieve this?
We are using Django with PostgreSQL on Heroku. We have tables with primary keys and unique constraints (created with class Meta / unique_together in Django). But the problem is, we found at least a few times that tables don't have primary keys and unique constraints - they disappeared for unknown reasons.
Does anyone know why the constraints disappeared? We didn't get any error messages but there were duplicate values in the tables where the unique constraints should have prevented them. The primary keys and unique constraints just disappeared.
What happened and how do we prevent it from happening again?